#!/bin/bash

export CROSSPATH=/opt/yi/toolchain-sunxi-musl/toolchain/bin
export PATH=${PATH}:${CROSSPATH}

export TARGET=arm-openwrt-linux
export CROSS=arm-openwrt-linux
export BUILD=x86_64-pc-linux-gnu

export CROSSPREFIX=${CROSS}-

export STRIP=${CROSSPREFIX}strip
export CXX=${CROSSPREFIX}g++
export CC=${CROSSPREFIX}gcc
export LD=${CROSSPREFIX}ld
export AS=${CROSSPREFIX}as
export AR=${CROSSPREFIX}ar

SCRIPT_DIR=$(cd `dirname $0` && pwd)
cd $SCRIPT_DIR

#export HAVE_WOLFSSL=1
#export HAVE_MBEDTLS=1
export USE_ZLIB=1

if [ "$HAVE_WOLFSSL" == "1" ]; then
    cd wolfssl
    ./autogen.sh
    ./configure --host=arm-openwrt-linux --enable-static --enable-base64encode \
        --disable-aescbc --disable-aesgcm --disable-aes --disable-md5 --disable-sha384 \
        --disable-sha512 --disable-chacha --disable-pwdbased \
        --disable-hkdf --disable-poly1305 --disable-rsa --disable-rsapss --disable-dh \
        --disable-tls13 --disable-pkcs8 --disable-crypttests --disable-crypttests-libs
    CFLAGS="-ffunction-sections -fdata-sections" LDFLAGS="-Wl,--gc-sections" make -j $(nproc)
    cd ..
else
    if [ "$HAVE_MBEDTLS" == "1" ]; then
        cd mbedtls
        make clean
        CFLAGS="-ffunction-sections -fdata-sections" LDFLAGS="-Wl,--gc-sections" make -j $(nproc) no_test
        cd ..
    else
        cd libtomcrypt
        make clean
        CFLAGS="-DLTC_NOTHING -DLTC_SHA1 -DLTC_BASE64 -ffunction-sections -fdata-sections" LDFLAGS="-Wl,--gc-sections" make -j $(nproc)
        cd ..
    fi
fi

if [ "$USE_ZLIB" == "1" ]; then
    cd zlib
    export CHOST=--host=arm-openwrt-linux
    ./configure
    make -j $(nproc)
    export CHOST=
    cd ..
fi

cd json-c/build
cmake .. \
    -DCMAKE_TOOLCHAIN_FILE="$SCRIPT_DIR/arm-openwrt-linux-toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX=$SCRIPT_DIR/json-c/build/_install
make -j$(nproc)
make install
cd ../..

cd onvif_simple_server || exit 1

if [ -f Makefile.static ]; then
    mv Makefile.static Makefile
fi
make clean
make -j $(nproc) || exit 1

mkdir -p ../_install/www/onvif || exit 1
mkdir -p ../_install/bin || exit 1
mkdir -p ../_install/etc/onvif_notify_server || exit 1
mkdir -p ../_install/etc/wsd_simple_server || exit 1

cp ./onvif_simple_server ../_install/www/onvif || exit 1
echo "#!/tmp/sd/yi-hack/www/onvif/onvif_simple_server" > ../_install/www/onvif/device_service || exit 1
chmod 0755 ../_install/www/onvif/device_service
echo "#!/tmp/sd/yi-hack/www/onvif/onvif_simple_server" > ../_install/www/onvif/events_service || exit 1
chmod 0755 ../_install/www/onvif/events_service
echo "#!/tmp/sd/yi-hack/www/onvif/onvif_simple_server" > ../_install/www/onvif/media_service || exit 1
chmod 0755 ../_install/www/onvif/media_service
echo "#!/tmp/sd/yi-hack/www/onvif/onvif_simple_server" > ../_install/www/onvif/media2_service || exit 1
chmod 0755 ../_install/www/onvif/media2_service
echo "#!/tmp/sd/yi-hack/www/onvif/onvif_simple_server" > ../_install/www/onvif/ptz_service || exit 1
chmod 0755 ../_install/www/onvif/ptz_service
echo "#!/tmp/sd/yi-hack/www/onvif/onvif_simple_server" > ../_install/www/onvif/deviceio_service || exit 1
chmod 0755 ../_install/www/onvif/deviceio_service
cp -R ./device_service_files ../_install/www/onvif
cp -R ./events_service_files ../_install/www/onvif
cp -R ./generic_files ../_install/www/onvif
cp -R ./media_service_files ../_install/www/onvif
cp -R ./media2_service_files ../_install/www/onvif
cp -R ./ptz_service_files ../_install/www/onvif
cp -R ./deviceio_service_files ../_install/www/onvif
if [ "$USE_ZLIB" == "1" ]; then
    PROJ_PWD=$(pwd)
    for DIR in device_service deviceio_service events_service generic media_service media2_service ptz_service; do
        cd ../_install/www/onvif/${DIR}_files
        rm -f *.gz
        for FILE in *.xml; do gzip $FILE; done;
        cd $PROJ_PWD
    done
fi

cp ./onvif_notify_server ../_install/bin || exit 1
cp -R ./notify_files/* ../_install/etc/onvif_notify_server

cp ./wsd_simple_server ../_install/bin || exit 1
cp -R ./wsd_files/* ../_install/etc/wsd_simple_server

${STRIP} ../_install/bin/* || exit 1
${STRIP} ../_install/www/onvif/onvif_simple_server || exit 1
